<template>
    <div class="orderReserveContainer">
        <div class="orderReserveTop">
            <img src="@/assets/back.png" alt="" @click="()=>{router.go(-1)}">
            <div class="pageName">Đơn hẹn</div>
            <div class="customer"></div>
        </div>
        <div class="orderReserveContent">
            <div class="reserveList" v-if="reserveData.reserveList.length > 0">
                <div class="reserveItem" v-for="(reserveItem, index) in reserveData.reserveList" :key="index">
                    <div class="itemTop">
                        <div class="iconAndAppName">
                            <img :src="reserveItem.reserveLogo" alt="">
                            <p class="appName">{{ reserveItem.reserveName }}</p>
                        </div>
                        <div class="reserveStatus">chờ giải ngân</div>
                    </div>

                    <div class="itemContent">
                        <div class="moneyArea">
                            <p class="moneyDesc">
                                Số tiền vay ước tính
                            </p>
                            <p class="moneyNum">
                                {{ Number(reserveItem.reserveAmt).toLocaleString() }}
                            </p>
                        </div>
                        <div class="dateArea">
                            <p class="datetitle">Ngày nộp đơn</p>
                            <p class="dateText">{{ reserveItem.reserveTime }}</p>
                        </div>
                        <div class="tipArea">
                            {{ reserveData.reserveTips }}
                        </div>
                    </div>

                    <div class="cancelReserveBtn" @click="clickItemCancelHandler(reserveItem.reserveId)">
                        Huỷ đơn hẹn
                    </div>


                </div>
            </div>
            <div class="emptyPage" v-else>
                <img src="@/assets/emptyImg.png" alt="">
            </div>
        </div>


        <div class="dialogContainer" v-if="showDialog">
            <div class="dialogContent">
                <p class="dialogContext">Xác nhận rằng bạn muốn bỏ tiền?</p>
                <div class="dialogBtns">
                    <p class="dialogCancel" @click="cancelClickHandler">Từ bỏ</p>
                    <p class="dialogNoCancel" @click="noCancelHandler">Nghĩ lại đi</p>
                </div>
            </div>

        </div>
    </div>
</template>

<script setup lang='ts'>
import api, { type ReserveDataModel } from '@/api';
import router from '@/router';
import { getItem } from '@/utils/token-utils';
import { Toast } from 'tdesign-mobile-vue';
import { onMounted, ref } from 'vue';


let showDialog = ref(false)

const initReserveData = () => ({
    reserveList: [],
    reserveTips: ''
})

let reserveData = ref<ReserveDataModel>(initReserveData())

// current click cancel order
let currentReserveOrderId = ref('')

// click big cancel btn handler 
const clickItemCancelHandler = (reserveOrderId: string) => {
    currentReserveOrderId.value = reserveOrderId
    showDialog.value = true
}

// confirm cancel reserveOrder
const cancelClickHandler = async () => {
    try {
        let cancelRes = (await api.cancelReserveOrder({
            uuid:getItem('uuid'),
            token:getItem('token'),
            reserveId:currentReserveOrderId.value
        })).data

        if (cancelRes.code==0) {
            // getReserveOrderListHandler()
            window.location.reload()
        }else{
            Toast({
                message:cancelRes.message,
                duration:3*1000
            })
        }
        
    } catch (error) {
        console.log(error);
    }
    noCancelHandler()
}

// noConfirm cancel reserveOrder
const noCancelHandler = () => {
    showDialog.value = false
}

const getReserveOrderListHandler = async () => {
    try {
        let reserveListRes = (await api.getReserveOrderList({
            uuid: getItem('uuid'),
            token: getItem('token')
        })).data

        if (reserveListRes.code == 0) {
            console.log(reserveListRes);
            if (reserveListRes.data) {
                reserveData.value = reserveListRes.data
            }
        } else {
            Toast({
                message: reserveListRes.message,
                duration: 3 * 1000
            })
        }
    } catch (error) {
        console.log(error);

    }
}

onMounted(() => {
    getReserveOrderListHandler()
})
</script>

<style lang="less" scoped>
.orderReserveContainer {
    height: 100%;
    display: flex;
    flex-direction: column;
    background-color: #F7F7F9;

    .orderReserveTop {
        min-height: 2.3rem;
        padding: 0 .8rem;
        display: flex;
        justify-content: space-between;
        align-items: center;

        &>img {
            display: block;
            width: 1.2rem;
            height: 1.2rem;
        }

        .pageName {
            font-family: SF Pro, SF Pro;
            font-weight: 600;
            font-size: 18px;
            color: #292B33;
            line-height: 26px;
            text-align: left;
            font-style: normal;
            text-transform: none;
        }

        .customer {
            width: 1.2rem;
            height: 1.2rem;
        }
    }

    .orderReserveContent {
        width: 100%;
        flex: 1;
        overflow: scroll;
        padding: .4rem .8rem;

        .reserveList {
            .reserveItem {
                width: 100%;
                background-color: #fff;
                border-radius: .4rem;
                padding: .8rem;
                margin-bottom: .6rem;

                .itemTop {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    border-bottom: 1px solid #DCDCE0;
                    padding-bottom: .6rem;

                    .iconAndAppName {
                        display: flex;
                        justify-content: flex-start;
                        align-items: center;

                        &>img {
                            display: block;
                            width: 1.2rem;
                            height: 1.2rem;
                            margin-right: .45rem;
                        }

                        .appName {
                            font-family: SF Pro, SF Pro;
                            font-weight: 600;
                            font-size: .7rem;
                            color: #292B33;
                            line-height: 1.1rem;
                            text-align: center;
                            font-style: normal;
                            text-transform: none;
                        }
                    }

                    .reserveStatus {
                        font-family: SF Pro, SF Pro;
                        font-weight: 400;
                        font-size: .7rem;
                        color: #FF9326;
                        line-height: 1.1rem;
                        text-align: center;
                        font-style: normal;
                        text-transform: none;
                    }
                }

                .itemContent {
                    padding-top: .6rem;

                    .moneyArea {
                        .moneyDesc {
                            font-family: SF Pro, SF Pro;
                            font-weight: 400;
                            font-size: .7rem;
                            color: #626673;
                            line-height: 1.1rem;
                            text-align: left;
                            font-style: normal;
                            text-transform: none;
                        }

                        .moneyNum {
                            font-family: DIN Alternate, DIN Alternate;
                            font-weight: bold;
                            font-size: 1.4rem;
                            color: #292B33;
                            line-height: 1.65rem;
                            text-align: left;
                            font-style: normal;
                            text-transform: none;
                            margin: .4rem 0;
                        }
                    }

                    .dateArea {
                        display: flex;
                        justify-content: space-between;
                        align-items: center;

                        font-family: SF Pro, SF Pro;
                        font-weight: 400;
                        font-size: .7rem;
                        color: #9599A6;
                        line-height: 1.1rem;
                        text-align: left;
                        font-style: normal;
                        text-transform: none;

                        .dateText {
                            color: #626673;
                        }
                    }

                    .tipArea {
                        padding: .4rem .6rem;
                        background-color: #FFF7EB;
                        border-radius: .2rem;
                        border: 1px solid #F1831D;
                        margin-top: .4rem;

                        font-family: SF Pro, SF Pro;
                        font-weight: 400;
                        font-size: .7rem;
                        color: #FF9326;
                        line-height: 1.1rem;
                        text-align: left;
                        font-style: normal;
                        text-transform: none;
                    }
                }
            }
        }

        .emptyPage{
            width: 100%;
            height: 80%;
            display: flex;
            justify-content: center;
            align-items: center;
            &>img{
                display: block;
                width: 6.8rem;
                height: 6.8rem;
            }
        }
    }

    .cancelReserveBtn {
        width: 100%;
        padding: .5rem;
        margin-top: .6rem;
        display: flex;
        justify-content: center;
        align-items: center;
        background-color: #F7F7FA;
        border-radius: .2rem;

        font-family: SF Pro, SF Pro;
        font-weight: 400;
        font-size: .8rem;
        color: #9599A6;
        line-height: 1.2rem;
        text-align: left;
        font-style: normal;
        text-transform: none;
    }

    .dialogContainer {
        position: absolute;
        left: 0;
        top: 0;
        right: 0;
        bottom: 0;
        background-color: rgba(0, 0, 0, .5);
        z-index: 999;
        display: flex;
        justify-content: center;
        align-items: center;

        .dialogContent {
            width: 85%;
            background-color: white;
            border-radius: .6rem;
            padding: 1rem;

            .dialogContext {
                font-family: SF Pro, SF Pro;
                font-weight: 600;
                font-size: 1rem;
                color: #292B33;
                line-height: 1.5rem;
                text-align: center;
                font-style: normal;
                text-transform: none;
            }

            .dialogBtns {
                display: flex;
                justify-content: space-between;
                margin-top: 1.2rem;

                &>p {
                    width: 48%;
                    font-weight: 600;
                    font-size: .8rem;
                    color: #626673;
                    line-height: 1.2rem;
                    text-align: center;
                    font-style: normal;
                    text-transform: none;
                    background-color: #F7F7FA;
                    padding: .5rem 0;
                    border-radius: .2rem
                }

                .dialogNoCancel {
                    background-color: #009959;
                    color: #fff;
                }
            }


            .dialogConfirmBtn {

                height: 2.2rem;
                background-color: #009959;
                display: flex;
                align-items: center;
                justify-content: center;
                border-radius: .2rem;

                /* font-family: SF Pro, SF Pro; */
                font-weight: 600;
                font-size: .8rem;
                color: #FFFFFF;
                line-height: 1.2rem;
                text-align: left;
                font-style: normal;
                text-transform: none;
            }
        }



    }

}
</style>